package com.example.book_shop_java.controller;

import com.alibaba.fastjson.JSON;
import com.example.book_shop_java.bean.WxUser;
import com.example.book_shop_java.dao.WxUserDao;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class WxUserController {
    @Resource
    WxUserDao wxUserDao;

    @RequestMapping("/wxLogin")
    public String wxLogin(@RequestParam("wx_username") String wx_username,
                          @RequestParam("wx_password") String wx_password){
        int i = wxUserDao.wxLogin(wx_username,wx_password);
        List<WxUser> wxUsers = wxUserDao.getLoginUser(wx_username,wx_password);
        System.out.println(wxUsers);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("user",wxUsers);
        res.put("message",message);
        res.put("code",code);
        String wxLoginMessage = JSON.toJSONString(res);
        return wxLoginMessage;
    }

    @RequestMapping("/wxRegiest")
    public String  wxRegiest(@RequestParam("wx_avatar") String wx_avatar,
                             @RequestParam("wx_username") String wx_username,
                             @RequestParam("wx_password") String wx_password,
                             @RequestParam("wx_phone") String wx_phone,
                             @RequestParam("wx_nickname") String wx_nickname
    ){
        int a = wxUserDao.getUsername(wx_username);
        if (a > 0) {
            int code = 500;
            HashMap<String, Object> res = new HashMap<>();
            String message = "账号已被注册";
            res.put("message",message);
            res.put("code",code);
            String wxRegiestMessage = JSON.toJSONString(res);
            return wxRegiestMessage;
        }
        int i = wxUserDao.wxRegiest(wx_username,wx_password,wx_phone,wx_avatar,wx_nickname);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("message",message);
        res.put("code",code);
        String wxRegiestMessage = JSON.toJSONString(res);
        return wxRegiestMessage;
    }

    @RequestMapping("/getAllWxUser")
    public String getAllWxUser(@RequestParam("wx_username") String wx_username){
        System.out.println(wx_username);
        List<WxUser> users = wxUserDao.getAllWxUser("%"+wx_username+"%");
        int totalSize = users.size();
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",users);
        res.put("totalSize",totalSize);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @RequestMapping("/resetPassword")
    public String resetPassword(int wx_userid){
        int i = wxUserDao.resetPassword(wx_userid);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("message",message);
        res.put("code",code);
        String updataPasswordMessage = JSON.toJSONString(res);
        return updataPasswordMessage;
    }
}
